package chapter11;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-04-16 下午2:55
 */
public class JdbcInterceptor implements Interceptor {
    private DataSource ds;
    private Connection conn;

    public JdbcInterceptor(DataSource ds) {
        this.ds = ds;
    }

    @Override
    public void before(Object object) throws SQLException {
        this.conn = ds.getConnection();
    }

    @Override
    public void after(Object object) {

    }

    @Override
    public void afterReturning(Object object) throws SQLException {
        conn.commit();
    }

    @Override
    public void afterThrowing(Object object) throws SQLException {
        conn.rollback();
    }
}
